using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Runtime.InteropServices;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MultiVMR9cs
{
    public class ImageSource : VideoSource
    {
        string _path;
        Bitmap _image;
        Texture _privateTexture;

        public ImageSource(MultiVMR9Renderer parentRenderer, string filename)
            : base(parentRenderer)
        {

            _path = filename;
            _image = new Bitmap(filename);

            // create and load the image into a texture
            OnDeviceReset();

            // create the default 3d mesh
            InitVertices();
        }

        public override Texture Texture
        {
            get
            {
                return _privateTexture;
            }
        }

        public override Size TextureSize
        {
            get
            {
                return _image.Size;
            }
        }

        public override void OnDeviceLost()
        {

        }

        public override void OnDeviceReset()
        {
            // reload the image texture
            _privateTexture = TextureLoader.FromFile(ParentRenderer.Device,
                _path,
                _image.Width,
                _image.Height,
                0,
                Usage.None,
                Format.A8R8G8B8,
                Pool.Managed,
                Filter.None,
                Filter.Linear,
                0);
        }

        public override int OnDraw()
        {
            int hr = DrawDefaultMesh(_privateTexture);
            return hr;
        }

        public override void Dispose()
        {
            if (_privateTexture != null)
            {
                _privateTexture.Dispose();
                _privateTexture = null;
            }
        }
    }
}
